package com.ricohgag.controller.home;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ricohgag.entity.News;
import com.ricohgag.service.NewsCatalogService;
import com.ricohgag.service.NewsService;
import com.ricohgag.util.vo.NewsCatalogVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author ricohgag
 * @date 2018/9/5 16:24
 */
@Controller
@RequestMapping("/home/news")
public class HomeNewsController {
    @Autowired
    private NewsCatalogService newsCatalogService;
    @Autowired
    private NewsService newsService;

    @RequestMapping("/index")
    public ModelAndView index(HttpServletRequest request, @RequestParam(value = "id", defaultValue = "0") Integer cId, @RequestParam(value = "pageNow" ,required = false) Integer current, String name) throws Exception{
        ModelAndView mav = new ModelAndView("/home/news/index");

        String url = request.getContextPath()+"/home/news/index";

        if(current == null){
            current = 1;
        }
        Page<News> page = new Page<>(current, 10);
        IPage<News> newsIPage = newsService.selectNewsPage(name, cId, page);

        List<NewsCatalogVo> newsCatalogVos = newsCatalogService.selectNewsCatalogVo();

        mav.addObject("navActive", 4);
        mav.addObject("name",name);
        mav.addObject("Pager", newsIPage);
        mav.addObject("pages", newsIPage.getPages());
        mav.addObject("newsCatalogVos", newsCatalogVos);
        mav.addObject("url",url);
        return mav;
    }

    @RequestMapping("/detail")
    public ModelAndView detail(Integer id) throws Exception{
        ModelAndView mav = new ModelAndView("/home/news/detail");
        News news = newsService.seleNewsOne(id);
        List<NewsCatalogVo> newsCatalogVos = newsCatalogService.selectNewsCatalogVo();
        System.err.println(newsCatalogVos);

        mav.addObject("newsCatalogVos", newsCatalogVos);
        mav.addObject("news", news);
        return mav;
    }


}
